//
//  ServiceMercPlatFormVC.swift
//  kxf
//
//  Created by Mac on 2024/3/24.
//

import Foundation
import MJRefresh
import MCToast

class ServiceMercPlatFormVC: WHBaseViewVC {
    
    var pageNum = 1
    var pageSize = 5
    
    var dataSourceArray = NSMutableArray()
    var dataDict = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendDataListRequest()
    }
    lazy var noticeVm : PerformanceNoticeVM = {
        let vm = PerformanceNoticeVM.init(frame: .zero)
        
        return vm
    }()
    lazy var todayPerformanceVm : PerformanceTodayVM = {
//        let vm = PerformanceTodayVM.init(frame: CGRect.init(x: 0, y: kFitWidth(58), width: 0, height: 0))
        let vm = PerformanceTodayVM.init(frame: CGRect.init(x: 0, y: kFitWidth(0), width: 0, height: 0))
        vm.tapBlock = {()in
            let vc = PerformanceVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm 
    }()
    lazy var dataTitleVm : PerformanceNoticeVM = {
        let vm = PerformanceNoticeVM.init(frame: CGRect.init(x: 0, y: self.todayPerformanceVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        vm.titleVm.titleLabel.text = "商户数据"
        vm.contentLabel.text = "时间倒序"
        vm.arrowImgView.setImgLocal(imgName: "arrow_img_down")
        
        return vm
    }()
    lazy var tableView : UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: self.dataTitleVm.frame.maxY+kFitWidth(10), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-getNavigationBarHeight()-(self.dataTitleVm.selfHeight+kFitWidth(10))), style: .plain)
        vi.delegate = self
        vi.dataSource = self
        vi.separatorStyle = .none
        vi.backgroundColor = .COLOR_LIGHT_GREY
        vi.showsVerticalScrollIndicator = false
        
        vi.register(ServicePlatFormTableViewCell.classForCoder(), forCellReuseIdentifier: "ServicePlatFormTableViewCell")
        
        if #available(iOS 11.0, *) {
            vi.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        vi.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendDataListRequest()
        })

        vi.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendDataListRequest()
        })
        
        return vi
    }()
    lazy var emptyView : EmptyViewWithBtn = {
        let emptyViewT = EmptyViewWithBtn.init(frame: CGRect.zero)
        emptyViewT.isHidden = true
        emptyViewT.contentLabel.text = "还没有记录~"
        
        return emptyViewT
    }()
}

extension ServiceMercPlatFormVC{
    func initUI() {
        initNavi(titleStr: "服务商平台")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(scrollView)
        scrollView.frame = CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(1), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-(getNavigationBarHeight()+kFitWidth(1)))
//        scrollView.addSubview(noticeVm)
        scrollView.addSubview(todayPerformanceVm)
        
        scrollView.addSubview(dataTitleVm)
        scrollView.addSubview(tableView)
        
        tableView.addSubview(emptyView)
        emptyView.center = CGPoint.init(x: SCREEN_WIDHT*0.5, y: self.tableView.frame.height*0.5)
        
        scrollView.contentSize = CGSize.init(width: 0, height: self.tableView.frame.maxY)
    }
}

extension ServiceMercPlatFormVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        emptyView.isHidden = dataSourceArray.count > 0 ? true : false
        return dataSourceArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ServicePlatFormTableViewCell") as! ServicePlatFormTableViewCell
        
        let dict = self.dataSourceArray[indexPath.row]as? NSDictionary ?? [:]
        cell.updateUI(dict: dict)
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kFitWidth(141)
    }
}

extension ServiceMercPlatFormVC{
    func sendDataListRequest() {
        MCToast.mc_loading()
        let urlString = "\(URL_appAgentHomeStatQb)?agentId=\(UserModel.shared.agentId)&pageNum=\(pageNum)&pageSize=\(pageSize)"
        WHNetworkUtil.shareManager().GET(urlString: urlString) { responseObject in
            DLLog(message: "\(responseObject)")
            MCToast.mc_remove()
            self.tableView.mj_footer!.endRefreshing()
            self.tableView.mj_header!.endRefreshing()
            
            if self.pageNum == 1{
                self.dataSourceArray.removeAllObjects()
            }
            
            let data = responseObject["data"]as? NSDictionary ?? [:]
            let dataArr = data["list"]as? NSArray ?? []
            
            self.dataDict = data["obj"]as? NSDictionary ?? [:]
            self.dataSourceArray.addObjects(from: dataArr as! [Any])
            // 提示没有更多数据
            if dataArr.count < self.pageSize {
                self.tableView.mj_footer!.endRefreshingWithNoMoreData()
            }
            
            self.tableView.reloadData()
            self.todayPerformanceVm.updateUI(dict: data["obj"]as? NSDictionary ?? [:])
        }
    }
}
